package com.dankegongyu.lib.common.util.fresco;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.view.SimpleDraweeView;

/**
 * Created by zhangbenzhi on 2018/3/27.
 * <p>
 * 圆角SimpleDraweeView：
 */

public class RoundedDraweeView extends SimpleDraweeView {

    private int borderRadius = 0;

    public RoundedDraweeView(Context context, GenericDraweeHierarchy hierarchy) {
        super(context, hierarchy);
    }

    public RoundedDraweeView(Context context) {
        super(context);
    }

    public RoundedDraweeView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoundedDraweeView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public RoundedDraweeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    /**
     * Drawable对象与Bitmap对象之间的转换：
     */
    public static Bitmap drawableToBitmap(Drawable drawable, boolean isHaveAlpah) {
        Bitmap bitmap;
        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if (bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        //ARGB_8888:A-透明度，占8位；R-红色，占8位；G-绿色，占8位；B-蓝色，占8位；一共是32位，即存储一像素的图需要占用32/8=4字节；
        //RGB_565：不存储透明度；R-5位，G-6位，B-5位，一共是16位，一像素占用内存为16/8=2字节；
        //ARGB_4444:一共占4*4=16位，2字节，不推荐使用因为失真严重；
        //ALPHA_8：只存储透明度，8位，1字节；

        Bitmap.Config config = Bitmap.Config.ARGB_8888;

        if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, config); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), config);
        }
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawRadius(canvas);
    }

    public void setBorderRadius(int borderRadius) {
        this.borderRadius = borderRadius;
    }

    /**
     * 画圆角图片
     */
    @SuppressLint({"ResourceAsColor", "WrongConstant"})
    private void drawRadius(Canvas canvas) {
        try {
            Drawable drawable = getDrawable();
            if (drawable == null) {
                return;
            }
            if (getWidth() == 0 || getHeight() == 0) {
                return;
            }
            Bitmap bitmap = drawableToBitmap(drawable, true);
            Paint paint = new Paint();
            paint.setColor(0xffffffff);
            paint.setAntiAlias(true);
            PorterDuffXfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
            canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            RectF rectf = new RectF(1, 1, getWidth() - 1, getHeight() - 1);
            canvas.drawRoundRect(rectf, borderRadius, borderRadius, paint);
            paint.setXfermode(xfermode);
            float scaleWidth = ((float) getWidth()) / bitmap.getWidth();
            float scaleHeight = ((float) getHeight()) / bitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            canvas.drawBitmap(bitmap, 0, 0, paint);
            canvas.restore();
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }
}
